package jmcnet.libcommun.utilit.evaluator;

public class ParsedExpression {
	private ExpressionParser.operateur operateur=null;
	private ParsedExpression expGauche=null;
	private ParsedExpression expDroite=null;
	
	private String variable=null;
	private ExpressionParser.constante constante = null;
	
	public static enum typeExpression {
		videExpression, operateurExpression, constanteExpression, variableExpression
	}
	
	private typeExpression type=null;

	public ParsedExpression() {
		this.type = typeExpression.videExpression;
	}
	
	public ParsedExpression(ParsedExpression expGauche, ExpressionParser.operateur operateur, ParsedExpression expDroite) {
		this.type = typeExpression.operateurExpression;
		this.operateur = operateur;
		this.expGauche = expGauche;
		this.expDroite = expDroite;
	}
	
	public ParsedExpression(String variable) {
		this.type = typeExpression.variableExpression;
		this.variable = variable;
	}
	
	public ParsedExpression(ExpressionParser.constante constante) {
		this.type = typeExpression.constanteExpression;
		this.constante = constante;
	}
	
	public ParsedExpression getExpressionGauche() throws ExpressionParserException {
		if (type != typeExpression.operateurExpression) {
			throw new ExpressionParserException("Expression is not a operator expression (maybe a constante or a variable)");
		}
		return expGauche;
	}
	
	public ParsedExpression getExpressionDroite() throws ExpressionParserException {
		if (type != typeExpression.operateurExpression) {
			throw new ExpressionParserException("Expression is not a operator expression (maybe a constante or a variable)");
		}
		return expDroite;
	}
	
	public ExpressionParser.operateur getExpressionOperateur() throws ExpressionParserException {
		if (type != typeExpression.operateurExpression) {
			throw new ExpressionParserException("Expression is not a operator expression (maybe a constante or a variable)");
		}
		return operateur;
	}
	
	public ExpressionParser.constante getConstante() throws ExpressionParserException {
		if (type != typeExpression.constanteExpression) {
			throw new ExpressionParserException("Expression is not a constant expression (maybe a expression or a variable)");
		}
		return constante;
	}
	
	public String getVariable() throws ExpressionParserException {
		if (type != typeExpression.variableExpression) {
			throw new ExpressionParserException("Expression is not a variable expression (maybe a expression or a constante)");
		}
		return variable;
	}
	
	public typeExpression getType() { return type;}
	
	public String toString() {
		StringBuffer sb = new StringBuffer();
		switch (type) {
		case operateurExpression :
			sb.append("[ ").append(operateur).append(", ").append(expGauche.toString()).append(" , ").append(expDroite.toString()).append(" ]");
			break;
		case constanteExpression :
			sb.append(constante);
			break;
		case variableExpression :
			sb.append("$").append(variable);
			break;
		default :
			sb.append("?").append(type).append("?");
		}
		
		return sb.toString();
	}
}
